package com.shd.edanpei.presenter;

import android.app.Dialog;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.ProductBean;
import com.shd.edanpei.presenter.contract.AddCarProductContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/12.
 * yang-wen-wen
 */

public class AddCarProductPresenter extends BasePresenterImpl<AddCarProductContract.AddCarProductIView>
        implements AddCarProductContract.AddCarProductIPresenter {

    private Dialog addressDialog;

    private ProductBean bean = new ProductBean();

    public AddCarProductPresenter(AddCarProductContract.AddCarProductIView mView) {
        super(mView);
    }

    @Override
    public void setAreas(final TextView text) {
        if (addressDialog == null) {
            addressDialog = DialogUtil.multipleAddressPicker(mBaseIView.getContext(), new DialogUtil.MultipleListener() {
                @Override
                public void sure(String desc, List<String> code) {
                    bean.setAreaDesc(desc);
                    bean.setAreaCode(code);
                    mBaseIView.setText(text, desc);
                }
            });
        }
        addressDialog.show();
    }

    @Override
    public void setMaxAmount(final TextView text) {
        DialogUtil.moneyLimitDialog(mBaseIView.getContext(), "订单最高额度", Constant.Unit.WANYUAN.getUnit(), new DialogUtil.Listener() {

            @Override
            public void done(String data) {
                mBaseIView.setText(text, data + Constant.Unit.WANYUAN.getUnit());
                bean.setLoanMax(data);
            }
        });
    }

    @Override
    public void setAuditTime(String str) {
        bean.setLoanDay(str);
    }

    @Override
    public void setRate(final TextView text) {
        DialogUtil.rateListDialog(mBaseIView.getContext(), new DialogUtil.RateListener() {
            @Override
            public void done(String str, String min, String max) {
                mBaseIView.setText(text, str);
                bean.setRateMin(min);
                bean.setRateMax(max);
            }
        });
    }

    @Override
    public void setAge(final TextView text) {
        DialogUtil.ageListDialog(mBaseIView.getContext(), new DialogUtil.RateListener() {
            @Override
            public void done(String str, String min, String max) {
                mBaseIView.setText(text, str);
                bean.setAgeMin(min);
                bean.setAgeMax(max);
            }
        });
    }

    @Override
    public void setCarDistance(final TextView text) {
        DialogUtil.moneyLimitDialog(mBaseIView.getContext(), "车公里数", Constant.Unit.WANKM.getUnit(), new DialogUtil.Listener() {

            @Override
            public void done(String str) {
                mBaseIView.setText(text, str + Constant.Unit.WANKM.getUnit());
                bean.setTravelWkm(str);
            }
        });
    }

    @Override
    public void setCarAge(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "车年限", R.array.car_age, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setCartYear(data);
            }
        });
    }

    @Override
    public void setHouseAge(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "住房年限", R.array.house_age, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setRoomAge(data);
            }
        });
    }

    @Override
    public void setHouseSize(String str) {
        bean.setRoomSize(str);
    }

    @Override
    public void setRepayWay(String str) {
        bean.setRepayMode(str);
    }

    @Override
    public void setRepayDate(final TextView text) {
        DialogUtil.listDialog(mBaseIView.getContext(), "订单期限", R.array.repay_date, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                mBaseIView.setText(text, data);
                bean.setRepayTerm(data);
            }
        });
    }

    @Override
    public void setNeedData(String str) {
        bean.setLoanData(str);
    }

    @Override
    public void submitCar() {
        Map<String, Object> map = checkCarProductData();
        if (map != null) {
            ApiCreator.getApi().carProduct(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            RxBus.get().send(RxBusCode.NOTIFY_PRODUCT_LIST);
                            ToastUtil.showToast("新增成功");
                            mBaseIView.getContext().finish();
                        }
                    });
        }
    }

    @Override
    public void submitHouse() {
        Map<String, Object> map = checkCarProductData();
        if (map != null) {
            ApiCreator.getApi().houseProduct(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            RxBus.get().send(RxBusCode.NOTIFY_PRODUCT_LIST);
                            ToastUtil.showToast("新增成功");
                            mBaseIView.getContext().finish();
                        }
                    });
        }
    }

    /**
     */
    private Map<String, Object> checkCarProductData() {
//        if (Utils.checkNotNull(mData.getBank(), "请选择所属银行")) {
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getBankName(), "请填写支行")) {
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getLoanMax(), "请选择最高额度")) {
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getLoanDay(), "请填写预计放款时间")) {
//            return null;
//        }
//        if (TextUtils.isEmpty(mData.getRateMin()) || TextUtils.isEmpty(mData.getRateMax())) {
//            ToastUtil.showToast("请选择利率");
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getRepayMode(), "请选择归还方式")) {
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getRepayTerm(), "请选择订单期限还款期限")) {
//            return null;
//        }
//        if (TextUtils.isEmpty(mData.getAgeMin()) || TextUtils.isEmpty(mData.getAgeMax())) {
//            ToastUtil.showToast("请选择年龄要求");
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getTravelWkm(), "请选择车公里数")) {
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getCartYear(), "请选择车年限")) {
//            return null;
//        }
//        if (Utils.checkNotNull(mData.getLoanData(), "请选择所需材料")) {
//            return null;
//        }
        return GsonUtil.toMap(bean);
//        return null;
    }

}
